package com.shenxuchen.crm.service;

import com.shenxuchen.crm.constant.Constants;

import com.shenxuchen.crm.mapper.*;
import com.shenxuchen.crm.pojo.*;
import com.shenxuchen.crm.utils.UUIDUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ClueServiceImpl implements ClueService {

    @Resource
    private ClueMapper clueMapper;

    @Resource
    private ClueRemarkMapper clueRemarkMapper;

    @Resource
    private ClueActivityMapper clueActivityMapper;

    @Resource
    private CustomerMapper customerMapper;

    @Resource
    private CustomerRemarkMapper customerRemarkMapper;

    @Resource
    private ContactMapper contactMapper;

    @Resource
    private ContactRemarkMapper contactRemarkMapper;

    @Resource
    private ContactActivityMapper contactActivityMapper;

    @Resource
    private TransactionMapper transactionMapper;

    @Resource
    private TransactionHistoryMapper transactionHistoryMapper;


    public List getList(Map search) {
        return clueMapper.getList(search);
    }

    public List getRefActs(String clueId) {
        return clueActivityMapper.getByClueIdWithActivity(clueId);
    }

    public void refActs(String clueId, String[] actIds) {
        // 先删除关系
        clueActivityMapper.delByClueId(clueId);

        // 添加关系
        if (actIds != null && actIds.length > 0) {
            for (String actId : actIds) {
                clueActivityMapper.add(clueId, actId);
            }
        }
    }

    public void unRef(String clueId, String actId) {
        clueActivityMapper.del(clueId, actId);
    }

    public Clue getById(String id) {
        return clueMapper.getById(id);
    }

    public void convert(String clueId, Boolean createTran, Transaction transaction, User user) {
        // 查询线索信息
        Clue clue = clueMapper.getById(clueId);

        // 查询线索备注
        List<ClueRemark> clueRemarks = clueRemarkMapper.getByClueId(clueId);

        // 查询线索和市场活动的关系
        List<ClueActivity> clueActivities = clueActivityMapper.getByClueId(clueId);


        //======================以下添加的数据都来自于线索相关===========================

        String createBy = user.getLoginAct() + "|" + user.getName();
        String createTime = Constants.SDF19.format(new Date());

        // 添加客户
        Customer customer = new Customer();
        customer.setId(UUIDUtils.getUUID());
        customer.setCreateBy(createBy);
        customer.setCreateTime(createTime);
        customer.setWebsite(clue.getWebsite());
        customer.setPhone(clue.getPhone());
        customer.setOwner(clue.getOwner());
        customer.setNextContactTime(clue.getNextContactTime());
        customer.setName(clue.getCompany());
        customer.setDescription(clue.getDescription());
        customer.setContactSummary(clue.getContactSummary());
        customer.setAddress(clue.getAddress());

        customerMapper.save(customer);

        // 添加客户备注
        if (clueRemarks != null && clueRemarks.size() > 0) {
            List<CustomerRemark> customerRemarks = new ArrayList<>();
            for (ClueRemark clueRemark : clueRemarks) {
                CustomerRemark customerRemark = new CustomerRemark();
                // 将第一个对象中的属性复制到第二个对象中类型和名字相同的属性中
                BeanUtils.copyProperties(clueRemark, customerRemark);
                customerRemark.setCustomerId(customer.getId());
                customerRemarks.add(customerRemark);
            }
            customerRemarkMapper.saveList(customerRemarks);
        }

        // 添加联系人
        Contact contact = new Contact();
        contact.setId(UUIDUtils.getUUID());
        contact.setSource(clue.getSource());
        contact.setOwner(clue.getOwner());
        contact.setNextContactTime(clue.getNextContactTime());
        contact.setMphone(clue.getMphone());
        contact.setJob(clue.getJob());
        contact.setFullName(clue.getFullName());
        contact.setEmail(clue.getEmail());
        contact.setCustomerId(customer.getId());
        contact.setCreateTime(createTime);
        contact.setCreateBy(createBy);
        contact.setAppellation(clue.getAppellation());
        contactMapper.save(contact);

        // 添加联系人备注
        if (clueRemarks != null && clueRemarks.size() > 0) {
            List<ContactRemark> contactRemarks = new ArrayList<>();
            for (ClueRemark clueRemark : clueRemarks) {
                ContactRemark contactRemark = new ContactRemark();
                // 将第一个对象中的属性复制到第二个对象中类型和名字相同的属性中
                BeanUtils.copyProperties(clueRemark, contactRemark);
                contactRemark.setContactsId(contact.getId());
                contactRemarks.add(contactRemark);
            }
            contactRemarkMapper.saveList(contactRemarks);
        }

        // 添加联系人和市场活动的关系
        if(clueActivities != null && clueActivities.size() > 0) {
            List<ContactActivity> contactActivities = new ArrayList<>();
            for (ClueActivity clueActivity : clueActivities) {
                ContactActivity contactActivity = new ContactActivity();
                contactActivity.setActivityId(clueActivity.getActivityId());
                contactActivity.setContactsId(contact.getId());
                contactActivities.add(contactActivity);
            }

            contactActivityMapper.saveList(contactActivities);
        }


        if (createTran) {
            // 添加交易
            transaction.setSource(clue.getSource());
            transaction.setOwner(clue.getOwner());
            transaction.setNextContactTime(clue.getNextContactTime());
            transaction.setId(UUIDUtils.getUUID());
            transaction.setCustomerId(customer.getId());
            transaction.setCreateTime(createTime);
            transaction.setCreateBy(createBy);
            transaction.setContactSummary(clue.getContactSummary());

            transactionMapper.save(transaction);

            // 添加交易历史
            TransactionHistory transactionHistory = new TransactionHistory();
            transactionHistory.setTransactionId(transaction.getId());
            transactionHistory.setId(UUIDUtils.getUUID());
            transactionHistory.setExpectedClosingDate(transaction.getExpectedClosingDate());
            transactionHistory.setEditBy(createBy);
            transactionHistory.setEditTime(createTime);
            transactionHistory.setAmountOfMoney(transaction.getAmountOfMoney());
            transactionHistory.setStage(transaction.getStage());

            transactionHistoryMapper.save(transactionHistory);
        }

        // 删除线索备注
        clueRemarkMapper.deleteByClueId(clueId);

        // 删除市场活动的关系
        clueActivityMapper.delByClueId(clueId);

        // 删除线索
        clueMapper.delete(clueId);
    }
}
